package com.myt.matp.gui;

import com.myt.matp.core.Event;

public class UIItem extends UIBanner {

    public static final int LAYOUT_VIT = 1;
    public static final int LAYOUT_VTI = 2;
    public static final int LAYOUT_HIT = 3;
    public static final int LAYOUT_HTI = 4;

    private int m_Layout = LAYOUT_HIT;
    
    private UIImage m_Image = null;
    
    protected UIItem(int id, GUI gui) {
	super(id, gui);
	// Default aspect
	setBorder(0);
    }

    public int getType() {
	return UIWidget.ITEM;
    }

    protected void changeFocus(boolean value) {
	if (isFocused() != value) {
	    resetPaintText();
	}
	m_OnFocus = value;
    }

    public final void loadImage(String fullpath) {
	setImage(m_Gui.createUIImage(getId()));
	if (hasImage()) {
	    m_Image.load(fullpath);
	}
    }

    public final boolean hasImage() {
	return m_Image != null;
    }

    public final void setImage(UIImage image) {
	m_Image = image;
	m_Image.setBorder(0);
	m_Image.setMargin(0);
    }

    public final UIImage getImage() {
	return m_Image;
    }

    public final int getViewX() {
	int ret = super.getViewX();
	if (m_Image != null) {
	    switch (m_Layout) {
	    case LAYOUT_HIT:
		ret += m_Image.getWidth();
		break;
	    }

	}
	return ret;
    }

    public final int getViewY() {
	int ret = super.getViewY();
	if (m_Image != null) {
	    switch (m_Layout) {
	    case LAYOUT_VIT:
		ret += m_Image.getHeight();
		break;
	    }

	}
	return ret;
    }

    public final int getViewWidth() {
	int ret = super.getViewWidth();
	if (m_Image != null) {
	    switch (m_Layout) {
	    case LAYOUT_HIT:
	    case LAYOUT_HTI:
		ret -= m_Image.getWidth();
		break;
	    }

	}
	return ret;
    }

    public final int getViewHeight() {
	int ret = super.getViewHeight();
	if (m_Image != null) {
	    switch (m_Layout) {
	    case LAYOUT_VIT:
	    case LAYOUT_VTI:
		ret -= m_Image.getHeight();
		break;
	    }

	}
	return ret;
    }
    
    protected int getMinHeight() {
	int ret = super.getMinHeight();
	if (hasImage()) {
	    switch (m_Layout) {
	    case LAYOUT_VIT:
	    case LAYOUT_VTI:
		ret += m_Image.getHeight();
		break;
	    case LAYOUT_HIT:
	    case LAYOUT_HTI:
		ret = Math.max(ret, m_Image.getMinHeight());
	    }

	}
	return ret;
    }

    protected int getMaxHeight() {
	return getMinHeight();
    }

    public int getColor(int color) {
	int ret = 0;

	switch (color) {
	case COLOR_BACKGROUND:
	    ret = super.getColor(color);
	    if (isFocused()) {
		ret = super.getColor(COLOR_CURSOR);
	    }
	    break;
	case COLOR_FOREGROUND:
	    ret = m_Gui.getColorFocus(super.getColor(color), isFocused());
	    break;
	default:
	    ret = super.getColor(color);
	    break;
	}
	return ret;
    }

    protected void paintTextToImage(MGraphics g) {

	super.paintTextToImage(g);

	if (m_Image != null) {
	    m_Image.paint(g);
	}

    }

    public boolean dispatchEvent(final Event e) {
	boolean ret = true;
	if (!dispatchEventUIItem(e)) {
	    ret = super.dispatchEvent(e);
	}
	return ret;
    }

    private boolean dispatchEventUIItem(final Event e) {
	boolean ret = false;

	switch (e.getId()) {
	case Event.UI_KEY_RELEASED:
	    ret = true;
	    switch (e.getWParam()) {
	    case GUI.KEY_OK:
		postEvent(Event.UI_COMMAND, getId());
		break;
	    case GUI.KEY_RIGHT:
		postEvent(Event.UI_TAB, GUI.KEY_RIGHT);
		break;
	    case GUI.KEY_LEFT:
		postEvent(Event.UI_TAB, GUI.KEY_LEFT);
		break;
	    default:
		ret = false;
	    }
	    break;
	}

	return ret;
    }
}